package com.quanyan.place.web;

import com.quanyan.api.APIResponse;
import com.quanyan.place.entity.PlaceBuyContract;
import com.quanyan.place.entity.PlaceBuyContractDetail;
import com.quanyan.place.entity.apireq.ReqContractPay;
import com.quanyan.place.entity.apireq.ReqPlaceContractInfoVo;
import com.quanyan.place.service.PlaceBuyService;
import com.quanyan.place.wrapper.PlaceWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 场馆购买合同的api
 * Created by zhang on 16/4/15.
 */
@RestController
//@RequestMapping("/placeBuy")
@RequestMapping("/admin/api/place/buy")
public class PlaceBuyController {

    @Autowired
    private PlaceBuyService placeBuyService;

    @Autowired
    private PlaceWrapper placeWrapper;
    /**
     * 添加场馆购进合同信息(最新)
     * @param
     * @return
     */
    @RequestMapping(value="/add",method = RequestMethod.POST,produces = {"application/json; charset=UTF-8"})
    public APIResponse<?> add(@RequestBody @Valid PlaceBuyContract placeBuyContract){

        APIResponse result = placeWrapper.addPlaceBuyContract(placeBuyContract);

        return result;
    }

    /**
     * 通过场馆Id获取购进的场馆场地信息
     * @param placeId
     * @return
     */
    @RequestMapping(value="getBuyPlaceInfoById/{placeId}")
    public APIResponse<?> getBuyPlaceInfoById(@PathVariable("placeId") Integer placeId){

        return placeBuyService.getBuyPlaceInfoById(placeId);
    }

    /**
     * 通过场馆Id获取购进场馆场地合同信息
     * @param placeId
     * @return
     */
    @RequestMapping(value="getPlaceBuyContractInfoByPlaceId/{placeId}")
    public APIResponse<?> getPlaceBuyContractInfoByPlaceId(@PathVariable("placeId") Integer placeId){

        return placeBuyService.getPlaceBuyContractInfoByPlaceId(placeId);
    }

    /**
     * 通过场馆ID获取购进场馆场地时间列表
     * @param placeUnitId 场馆场地ID
     * @return
     */
    @RequestMapping(value="getPlaceUnitTimeList/{placeUnitId}")
    public APIResponse<?> getPlaceUnitTimeList(@PathVariable("placeUnitId") int placeUnitId){
        return placeBuyService.getPlaceUnitTimeList(placeUnitId);
    }

    @RequestMapping(value="/getContractList",method = RequestMethod.POST)
    public APIResponse<?> getContractList(@RequestBody ReqPlaceContractInfoVo reqVo){
        return APIResponse.returnSuccess(placeWrapper.getContractList(reqVo));
    }

    @RequestMapping(value="/getContractListEx",method = RequestMethod.POST)
    public APIResponse<?> getContractListEx(@RequestBody ReqPlaceContractInfoVo reqVo){
        return APIResponse.returnSuccess(placeWrapper.getContractListEx(reqVo));
    }

    /**
     * 通过Id拿到合同信息
     * @param id
     * @return
     */
    @RequestMapping(value="/getContractDataById",method = RequestMethod.GET)
    public APIResponse<?> getContractDataById(@RequestParam (value="id") String id){
        return APIResponse.returnSuccess(placeWrapper.getContractDataById(Integer.parseInt(id)));
    }

    @RequestMapping(value="/getPlaceBuyContractDetailByPlaceUnitId",method = RequestMethod.GET)
    public APIResponse<?> getPlaceBuyContractDetailByPlaceUnitId(@RequestParam(value = "placeUnitId",required = false) String placeUnitId, String contractId){
       Integer unitId=Integer.parseInt(placeUnitId);
       Integer cId=Integer.parseInt(contractId);
        PlaceBuyContractDetail vo=new PlaceBuyContractDetail();
        vo.setListDetail(placeWrapper.getPlaceBuyContractDetailByPlaceUnitId(unitId,cId));
        APIResponse result=APIResponse.returnSuccess(vo);
        return result;
    }

    @RequestMapping(value="/updatePlaceContratctInfo",method = RequestMethod.POST)
    public APIResponse<?> updatePlaceContratctInfo(@RequestBody ReqPlaceContractInfoVo reqVo){
           // Integer Id = Integer.parseInt(reqVo.getId());
          APIResponse res= placeWrapper.updatePlaceContratctInfo(reqVo);
            if (res.getErrcode()==100){
                return APIResponse.returnFail(res.getErrmsg());
            }
            return APIResponse.returnSuccess();
        }

    @RequestMapping(value="/deletePlaceContratctInfo",method = RequestMethod.POST)
    public APIResponse<?> deletePlaceContratctInfo(@RequestBody ReqPlaceContractInfoVo reqVo){
        // Integer Id = Integer.parseInt(reqVo.getId());
        placeWrapper.deletePlaceContratctInfo(reqVo);
        return APIResponse.returnSuccess();
    }

    /**
     * 合同审批
     * @param reqVo
     * @return
     */
    @RequestMapping(value="/verifyContratctInfo",method = RequestMethod.POST)
    public APIResponse<?>verifyContratctInfo(@RequestBody ReqPlaceContractInfoVo reqVo){
        // Integer Id = Integer.parseInt(reqVo.getId());
        APIResponse result = placeWrapper.verifyContratctInfo(reqVo);
        return result;
    }

    @RequestMapping(value="/stopPlaceContratct",method = RequestMethod.POST)
    public APIResponse<?> stopPlaceContratct(@RequestBody ReqPlaceContractInfoVo reqVo){
        // Integer Id = Integer.parseInt(reqVo.getId());
        placeWrapper.stopPlaceContratct(reqVo);
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value="/expotrContractList",method = RequestMethod.POST)
    public APIResponse<?> expotrContractList(@RequestBody ReqPlaceContractInfoVo reqVo){
        return APIResponse.returnSuccess(placeWrapper.expotrContractList(reqVo));
    }

    @RequestMapping(value="/updateContractStatus",method = RequestMethod.POST)
    public APIResponse<?> updateContractStatus(@RequestBody ReqPlaceContractInfoVo reqVo){
        // Integer Id = Integer.parseInt(reqVo.getId());
        placeWrapper.updateContractStatus();
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value="/listContractPay/{contractId}")
    public APIResponse<?> listContractPay(@PathVariable("contractId") int contractId){
        return placeWrapper.getContractPayList(contractId);
    }

    @RequestMapping(value="/insertContractPay",method = RequestMethod.POST)
    public APIResponse<?> insertContractPay(@RequestBody ReqContractPay reqVo){
        return placeWrapper.updateContractPay(reqVo);
    }

    @RequestMapping(value="/updateContractPay",method = RequestMethod.POST)
    public APIResponse<?> updateContractPay(@RequestBody ReqContractPay reqVo){
        return placeWrapper.updateContractPay(reqVo);
    }

    @RequestMapping(value="/deleteContractPay",method = RequestMethod.POST)
    public APIResponse<?> deleteContractPay(@RequestBody ReqContractPay reqVo){
        return placeWrapper.deleteContractPay(reqVo);
    }
}
